﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections.Generic;
using FreeBroker.Properties;
using System.Threading;

namespace FreeBroker
{
    public partial class BrokerMainForm : Form
    {

        private void processManager_DoWork(object sender, DoWorkEventArgs e)
        {
            bool firstRun = true;
            DateTime startTime = DateTime.Now;

            while (running)
            {
                DateTime currentTime = DateTime.Now;
                TimeSpan span = currentTime - startTime;
                int elapsedSecs = (int)span.TotalSeconds;
                int minutes = (int)this.numUpDownAutoUpdateInterval.Value;
                int waitSeconds = minutes * 60;
                if (autoUpdate && this.processes.Count > 0)
                {
                    SetToolStripStatus("Nächste automatische Aktualisierung in ", (waitSeconds - elapsedSecs) + " Sekunden");
                }
                else
                {
                    SetToolStripStatus("", "");
                }

                if (autoUpdate && (elapsedSecs > waitSeconds || firstRun))
                {
                    SetToolStripStatus("", "");
                    for (int i = 0; i < processList.Count; i++)
                    {
                        int procNumber = processList[i];
                        // Iterate through processes
                        if (!this.jobQueue.Contains(procNumber))
                        {
                            BrokerObject brokerObj = this.processes[procNumber];
                            if (brokerObj != null)
                            {
                                Console.WriteLine("Automatically adding process <" + brokerObj.Name + ">...");
                                this.jobQueue.Enqueue(procNumber);
                            }
                        }
                    }
                    startTime = DateTime.Now;
                }

                // Check if manually added jobs are existing in queue
                if (this.jobQueue.Count > 0)
                {
                    List<int> queueCopy = new List<int>();
                    queueCopy.AddRange(this.jobQueue);
                    this.jobQueue.Clear();
                    foreach (int procNumber in queueCopy)
                    {
                        // Start asynchron search process
                        try
                        {
                            ReloadChartThumb(procNumber);
                            if (firstRun)
                            {
                                // Fetch information before starting this
                                //CollectAdditionalInfo(this.processes[procNumber]);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                firstRun = false;
                Thread.Sleep(1000);
            }

            Console.WriteLine("Closing Process Manager...");
        }
    }
    
}
